﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ISIM
{
    public partial class PreferencesForm : Form
    {
        public PreferencesForm()
        {
            InitializeComponent();

            this.TopMost = Global.OpenForms.mainForm.TopMost;

            autoRespondCheckBox.Text = Global.LangManager.Translate("autoRespondOnAFKorDND");
            awayReasonLabel.Text = Global.LangManager.Translate("awayReason");
            preferencesToolTip.SetToolTip(awayReason, Global.LangManager.Translate("awayReasonMessage"));
            enableEmoticons.Text = Global.LangManager.Translate("emoticons");
            muteSounds.Text = Global.LangManager.Translate("muteSounds");
            runOnStartUp.Text = Global.LangManager.Translate("runOnStartUp");
            deleteConversations.Text = Global.LangManager.Translate("removeConv");
            saveButton.Text = Global.LangManager.Translate("save");
            changeFont.Text = Global.LangManager.Translate("changeFont");
            dontActivateChatWindow.Text = Global.LangManager.Translate("dontActivateChatWindow");
            myNickTextBox.Text = Global.myNick;

            if (Global.settingsDictionary["ISIMsettings"].ContainsKey("autoRespondCheckBox"))
                autoRespondCheckBox.Checked = bool.Parse(Global.settingsDictionary["ISIMsettings"]["autoRespondCheckBox"]);
            if (Global.settingsDictionary["ISIMsettings"].ContainsKey("awayReason"))
                awayReason.Text = Global.settingsDictionary["ISIMsettings"]["awayReason"];
            if (Global.settingsDictionary["ISIMsettings"].ContainsKey("enableEmoticons"))
                enableEmoticons.Checked = bool.Parse(Global.settingsDictionary["ISIMsettings"]["enableEmoticons"]);
            else
                enableEmoticons.Checked = true;

            if (Global.settingsDictionary["ISIMsettings"].ContainsKey("muteSounds"))
                muteSounds.Checked = bool.Parse(Global.settingsDictionary["ISIMsettings"]["muteSounds"]);

            if (Global.settingsDictionary["ISIMsettings"].ContainsKey("dontActivateChatWindow"))
                dontActivateChatWindow.Checked = bool.Parse(Global.settingsDictionary["ISIMsettings"]["dontActivateChatWindow"]);

            runOnStartUp.Checked = Global.RegistryManager.IsOnStartup();


            languageSelector.SelectedIndex = Global.LangManager.Languages.IndexOf(Global.LangManager.currentCulture);

            SetDevices();
        }

        public void SetDevices()
        {
            if (Global.OpenForms.mainForm.skype == null) return;

            micDeviceSelector.Items.Clear();
            micDeviceSelector.Items.AddRange(Global.OpenForms.mainForm.skype.micDeviceList.ToArray());
            micDeviceSelector.SelectedItem = Global.OpenForms.mainForm.skype.mic;
            micDeviceSelector.Refresh();

            speakerDeviceSelector.Items.Clear();
            speakerDeviceSelector.Items.AddRange(Global.OpenForms.mainForm.skype.speakerDeviceList.ToArray());
            speakerDeviceSelector.SelectedItem = Global.OpenForms.mainForm.skype.speaker;
            speakerDeviceSelector.Refresh();

            videoDeviceSelector.Items.Clear();
            videoDeviceSelector.Items.AddRange(Global.OpenForms.mainForm.skype.webCamDeviceList.ToArray());
            videoDeviceSelector.SelectedItem = Global.OpenForms.mainForm.skype.myWebCam;
            videoDeviceSelector.Refresh();
        }

        private void saveButton_Click(object sender, System.EventArgs e)
        {
            if (myNickTextBox.Text.Replace(" ", "").Length == 0)
                myNickTextBox.Text = Global.LangManager.Translate("me");

            Global.myNick = myNickTextBox.Text;
            Global.ChangeISIMsettings("myNickTextBox", myNickTextBox.Text);
            Global.ChangeISIMsettings("autoRespondCheckBox", autoRespondCheckBox.Checked.ToString());
            Global.ChangeISIMsettings("awayReason", awayReason.Text);
            Global.ChangeISIMsettings("videoDeviceSelector", videoDeviceSelector.SelectedIndex.ToString());
            Global.ChangeISIMsettings("speakerDeviceSelector", speakerDeviceSelector.SelectedIndex.ToString());
            Global.ChangeISIMsettings("micDeviceSelector", micDeviceSelector.SelectedIndex.ToString());
            Global.ChangeISIMsettings("enableEmoticons", enableEmoticons.Checked.ToString());
            Global.ChangeISIMsettings("muteSounds", muteSounds.Checked.ToString());
            Global.ChangeISIMsettings("dontActivateChatWindow", dontActivateChatWindow.Checked.ToString());
            Global.RegistryManager.SetOnStartup(runOnStartUp.Checked);

            string culture = "en-GB";
            if (languageSelector.SelectedIndex != -1)
                culture = Global.LangManager.Languages[languageSelector.SelectedIndex];

            if (Global.LangManager.currentCulture != culture)
            {
                Global.LangManager.SaveLangToFile(culture);
                Global.LangManager.SetLanguage(culture);
                Global.OpenForms.mainForm.TranslateForm();
            }

            Close();
        }

        private void deleteConversations_Click(object sender, System.EventArgs e)
        {
            if (MessageBox.Show(Global.LangManager.Translate("reallyRemoveAllHistory"), Global.LangManager.Translate("remove") + "?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.Yes)
                Global.ISIMFunctions.DeleteConversations();
        }

        private void micDeviceSelector_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            if ((micDeviceSelector.SelectedItem == null) || (speakerDeviceSelector.SelectedItem == null)) return;
            Global.OpenForms.mainForm.skype.SetDevices(micDeviceSelector.SelectedItem as SkypeClass.AudioDevice, speakerDeviceSelector.SelectedItem as SkypeClass.AudioDevice);
        }

        private void videoDeviceSelector_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            if (videoDeviceSelector.SelectedItem == null) return;
            Global.OpenForms.mainForm.skype.SetVideoDevice(videoDeviceSelector.SelectedItem as SkypeClass.VideoDevice);
        }

        private void PreferencesForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            Global.OpenForms.preferencesForm = null;
        }

        private void changeFont_Click(object sender, EventArgs e)
        {
            FontDialog fd = new FontDialog();
            fd.Font = Global.chatFont;

            if (fd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Global.chatFont = fd.Font;
                Global.ChangeISIMsettings("chatFont", fd.Font.FontFamily.Name + "|" + fd.Font.Size + "|" + fd.Font.Style.ToString());
            }
        }
    }
}
